//
//  ProveCell.m
//  Hxcsfw
//
//  Created by qiysoft on 2018/2/1.
//  Copyright © 2018年 企友软mac. All rights reserved.
//

#import "ProveCell.h"

@implementation ProveCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code

    self.OKBtn.layer.masksToBounds = YES;
    self.OKBtn.layer.cornerRadius = 5;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

- (IBAction)OKBtn:(id)sender {//确认注册
    
    self.OKBtn.enabled = NO;
    
    [self DetermineRegistration];//确认注册

}
- (IBAction)CXFSBtn:(id)sender {//重发验证码
}

//确认注册
-(void)DetermineRegistration {

    if ([WShareUtil strNilOrEmpty:[WShareUtil sharedData].RegistSMSyzm]) {
        [WShareUtil showAlert:@"温馨提示" message:@"短信验证码错误，请重新输入！"];
        self.OKBtn.enabled = YES;
        return;
    }
    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"regist",@"content":@{@"revert":@"0",@"dto":@{@"phone":self.phoneLB.text,@"code":[WShareUtil sharedData].RegistSMSyzm}}};
    WLog(@"手机号：%@",self.phoneLB.text);
    WLog(@"验证码：%@",[WShareUtil sharedData].RegistSMSyzm);

    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};
    
    
    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];
        
        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];

        if ([returnDic[@"phone"] isEqualToString:self.phoneLB.text]) {
            //召唤主线程改变UI
            [self performSelectorOnMainThread:@selector(RegistSuccess) withObject:nil waitUntilDone:YES];
        }else{
            [WShareUtil showAlert:@"温馨提示" message:returnDic[@"errorInfo"]];
//            [self performSelectorOnMainThread:@selector(RegistSuccess) withObject:nil waitUntilDone:YES];

        }
        self.OKBtn.enabled = YES;

    } failure:^(NSError *error) {

        [WShareUtil showAlert:@"温馨提示" message:@"注册失败！"];
        
        self.OKBtn.enabled = YES;
        
    } showProgress:YES];
}

-(void)RegistSuccess {
    
    [SVProgressHUD showImage:nil status:@"注册成功"];
    [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
    [SVProgressHUD dismissWithDelay:1.];
    [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];   //背景颜色
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PhoneRegistSuccess" object:nil];
//    [WShareUtil sharedData].dicSession = returnDic;
}

@end
